home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Evaluation Requirement Question
- Date: Sat, 16 Mar 96 14:45:30 GMT
- Organization: none
- Distribution: world
- Message-ID: <826987530snz@genesis.demon.co.uk>
- References: <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
- c23kab@kocrsv08.delcoelect.com "Kirk Bailey" writes:
-
- > In the following example program, notice that the
- >test for "i" appears both before and after the call to "foo".
- >Note that the printf will only be executed (in this example)
- >due to the last "i != 0" check (i is initialized to 0, and
- >foo will return 1). My question: Is the compiler _required_
- >to evaluate "i" twice in all cases where it can not determine
- >that the call to "foo" did not alter "i"?
-
- The first thing to consider is that main can be called recursively so the
- compiler can't assume that when main is called that i will have its
- statically initialised value. Global analysis could determine that in this
- case assuming the compiler 'knows' about printf but it is extremely unlikely
- that a real compiler will do such an analysis.
-
- > Are there cases
- >where "i" doesn't have to be evaluated twice? Consider some
- >other possible programs:
-
- Clearly i won't be evaluated twice if foo() returns zero due to the
- short-circuiting properties of ||.
-
- A conforming compiler could inline the code of foo within main (there would
- still have to be a separate function generated since foo has external linkage).
-
- > 1) foo did not _change_ i, or
-
- If foo didn't change the value of i then the compiler could determine
- that the 2nd "i != 0" has no side-effects, always evaluates to 0 and
- hence doesn't have to evaluate it explicitly.
-
- > 2) foo did not _change_ i AND foo's definition occurred
- > before its invocation (allowing the compiler to "know"
- > this at the point of invocation), or
-
- The compiler can make use of foo's definition whether it occurs before or
- after foo. The compiler can do what it likes so long as it doesn't change
- the overall behaviour of the program.
-
- > 3) "i" was a local variable to main (explicitly init. to
- > zero) - meaning that no function outside of main could
- > alter it through the call to foo.
-
- That means that the compiler knows the value of i when the if statement
- is entered. Therefore it can determine trivially the result of the first
- i == 0 and possible the value of i throughout the entire expression. It might
- in that case be able to all 3 of the tests and depending on the
- result execute the body of the if statement unconditionally or eliminate
- it completely.
-
- However what compilers can do in theory and what they do in practice are
- entirely different things! It is certainly true however that local variables
- are much easier to optimise than global variables. For that reason and others
- you should use local variables where possible.
-
- >#include <stdio.h>
- >
- >int i;
- >int foo(void);
- >
- >int main(void)
- >{
- > if ( i != 0 || !foo() || i != 0 )
- > printf( "got here!\n" );
- > return 0;
- >}
- >
- >int foo(void)
- >{
- > i++;
- > return i;
- >}
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-